package com.google.code.mochaccino.framework.jmx;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import javax.management.Attribute;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.ReflectionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Attribute JMX Value
 *
 * @author choule
 */
public class JmxValueAttribute<T> implements JmxValue<T> {
	/** The Logger */
	private static final Logger logger = LoggerFactory.getLogger( JmxValueAttribute.class );

	/** The JMX Bean */
	private JmxBean bean = null;

	/** The JMX Connection */
	private JmxConnection connection = null;

	/** The Cached Data from last time */
	private Object cachedData = null;

	/** Attribute Name */
	private String attributeName = null;

	/** The name of the attribute */
	private String name = null;

	/** Cache the Data? */
	private boolean cacheData = true;

	public JmxValueAttribute() {
	}

	public String getAttributeName() {
		return attributeName;
	}

	public void setAttributeName( String methodName ) {
		this.attributeName = methodName;
	}

	public JmxBean getBean() {
		return bean;
	}

	public void setBean( JmxBean name ) {
		this.bean = name;
	}

	public JmxConnection getConnection() {
		return connection;
	}

	public void setConnection( JmxConnection connection ) {
		this.connection = connection;
	}

	/** @return the name */
	@Override
	public String getName() {
		return name;
	}

	/** @param name the name to set */
	public void setName( String name ) {
		this.name = name;
	}

	@Override
	@SuppressWarnings( "unchecked" )
	public T getValue() {
		if ( (cachedData != null) && cacheData ) {
			return (T) cachedData;
		}
		cachedData = getRawValue();
		return (T) cachedData;
	}

	protected Object getRawValue() {
		try {
			MBeanServerConnection conn = connection.getConnection();
			if ( conn != null ) {
				return conn.getAttribute( bean.getObjectName(), attributeName );
			}
		} catch ( AttributeNotFoundException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
			return null;
		} catch ( InstanceNotFoundException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
			return null;
		} catch ( MBeanException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
			return null;
		} catch ( ReflectionException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
			return null;
		} catch ( IOException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
		}
		return null;
	}

	public boolean isCacheData() {
		return cacheData;
	}

	public void setCacheData( boolean cacheData ) {
		this.cacheData = cacheData;
	}

	public boolean setValue( Object value ) {
		try {
			MBeanServerConnection conn = connection.getConnection();
			if ( conn != null ) {
				conn.setAttribute( bean.getObjectName(), new Attribute( attributeName, value ) );
				cachedData = null;
				return true;
			}
		} catch ( InstanceNotFoundException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
		} catch ( AttributeNotFoundException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
		} catch ( InvalidAttributeValueException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
		} catch ( MBeanException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
		} catch ( ReflectionException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
		} catch ( IOException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
		}
		cachedData = null;
		return false;
	}
}
